package com.mybed.crm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mybed.crm.anno.Log;
import com.mybed.crm.exception.BaseException;
import com.mybed.crm.pojo.dto.DeptDto;
import com.mybed.crm.pojo.dto.QueryDeptDto;
import com.mybed.crm.pojo.pojo.Dept;
import com.mybed.crm.pojo.pojo.User;
import com.mybed.crm.result.Result;
import com.mybed.crm.result.ResultP;
import com.mybed.crm.service.DeptService;
import com.mybed.crm.service.UserService;
import com.mybed.crm.utils.thread.UserThreadLocalUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/system/dept")
public class DeptController {
    @Autowired
    private DeptService deptService;
    @Autowired
    private UserService userService;
    @GetMapping("/treeselect")
    public Result treeselect(){
        return deptService.treeselect();
    }
    @GetMapping("/list")
    public Result deptList(QueryDeptDto dto){
        LambdaQueryWrapper<Dept> wrapper=new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(dto.getDeptName())){
            wrapper.like(Dept::getDeptName,dto.getDeptName());
        }
        if(dto.getStatus()!=null){
            wrapper.eq(Dept::getStatus,dto.getStatus());
        }
        return Result.success(deptService.list(wrapper));
    }

    /**
     * 新增部门
     * @param dto
     * @return
     */
    @Log("新增部门")
    @PostMapping
    public ResultP addDept(@RequestBody DeptDto dto){
        Dept one = deptService.getOne(Wrappers.<Dept>lambdaQuery().eq(Dept::getDeptName, dto.getDeptName()));
        if(one!=null){
            throw new BaseException("新增部门名字重复");
        }
        Dept dept=new Dept();
        BeanUtils.copyProperties(dto,dept);
        User user = userService.getById(UserThreadLocalUtil.getUser());
        dept.setCreateBy(user.getCreateBy());
        dept.setCreateTime(LocalDateTime.now());
        deptService.save(dept);
        return ResultP.success();
    }
    /**
     * 通过id查询部门
     */
    @GetMapping("/{DeptId}")
    public Result QueryDeptById(@PathVariable String DeptId){
        return Result.success(deptService.getById(DeptId));
    }
    /**
     * 通过id查询其余部门
     */
    @GetMapping("/list/exclude/{DeptId}")
    public Result QueryExDeptById(@PathVariable String DeptId){
        return Result.success(deptService.list(Wrappers.<Dept>lambdaQuery().ne(Dept::getDeptId,DeptId)));
    }

    /**
     * 修改部门
     * @param dept
     * @return
     */
    @Log("修改部门")
    @PutMapping
    public ResultP updateDept(@RequestBody Dept dept){
        return deptService.updateDept(dept);
    }
    /**
     * 删除部门
     */
    @Log("删除部门")
    @DeleteMapping("/{DeptId}")
    public ResultP deleteById(@PathVariable Integer DeptId){
        return deptService.deleteById(DeptId);
    }
}
